********************************
* IV and OLS Analysis for HMIS *
********************************
use "${build}/iv_fin_hmis_w_comp_weights.dta", clear
replace black = 0 if black_miss == 1
replace fem = 0 if fem_miss == 1

*Create datasets we will populate with results
cap frame change default
cap frame drop results 
frame create results strL(label) b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evic_all b_judgeamount se_judgeamount

**************************
* VARIABLE BUILDING *
**************************

foreach var of varlist n_em_shelter1_pre n_em_shelter2_pre n_em_shelter3_pre n_em_shelter4_pre   n_shelter1_pre n_shelter2_pre n_shelter3_pre n_shelter4_pre  {
	gen `var'_miss = `var' == .
	replace `var' = -1  if `var'_miss == 1
}

cap gen fem_hisp = hisp_fem
gen ageatcase_sq = ageatcase^2
gen ageatcase_cub = ageatcase^3

**************************
*Regression specification*
**************************
global cluster_ols="judgeXyear"
global cluster_iv="judgeXyear"
global stem = ""
global instrument="loo_vc_ivsamp100"
global fe="districtXyearXquarter"

*Outcomes
local outcomes_main = " n_em_shelter n_shelter "

*Define controls
macro define controls="noprior noprior_miss joint ad_damnum ageatcase ageatcase_sq ageatcase_cub fem black white fem_black fem_white hispanic fem_hisp fem_miss black_miss hispanic_miss nbrhd_pov_rate_5yr nbrhd_med_rent_5yr nbrhd_pov_rate_5yr_miss nbrhd_med_rent_5yr_miss"

*Define controls with lags
macro define main_lags = " n_em_shelter1_pre n_shelter1_pre n_em_shelter1_pre_miss n_shelter1_pre_miss n_em_shelter2_pre n_shelter2_pre n_em_shelter2_pre_miss n_shelter2_pre_miss "
macro define n1_lags = " n_em_shelter3_pre n_shelter3_pre n_em_shelter3_pre_miss n_shelter3_pre_miss n_em_shelter2_pre n_shelter2_pre n_em_shelter2_pre_miss n_shelter2_pre_miss "
macro define n2_lags = " n_em_shelter3_pre n_shelter3_pre n_em_shelter3_pre_miss n_shelter3_pre_miss n_em_shelter4_pre n_shelter4_pre n_em_shelter4_pre_miss n_shelter4_pre_miss "

*Define judge stringency along money judgment dimension
gen trim_amnt_loo_vc_ivsamp100_s = trimamountloo_vc_ivsamp100/1000
global amnt_instrument = "trim_amnt_loo_vc_ivsamp100_s"	


***********************
* MAIN IV REGRESSIONS *
***********************
gen all=1
gen sample_restriction =.
gen robust_samp=.
gen imp_robust_samp = . 

qui foreach pop in "all" "black" "fem"  {
	foreach popval of numlist 0 1 {
		if "`pop'"~="all" | `popval'~=0	{	
			  foreach var in `outcomes_main'  {
				foreach rng in  "1" "2" "3_4" "5_6" "1_pre"   {
				
				nois di " ON outcome: `var', pop: `pop'=`popval', time period: `rng' "
				if (inlist("`rng'", "1_pre", "2_pre") & "`pop'" != "all") continue
				*Set lags to missing if outcome window is a per-period
				if (inlist("`rng'","1", "2") | inlist("`rng'", "3_4", "5_6") )   local lags = " $main_lags "
				if ("`rng'" == "1_pre") local lags = " $n1_lags "
				if ("`rng'" == "2_pre" | "`rng'" == "1_2pre") local lags = " $n2_lags "
				
				* Dealing with variables that are controls in some regs and outcomes in some regs
				if inlist("`rng'","1_pre" ,"2_pre") {
					replace `var'`rng' = . if `var'`rng'_miss == 1
				}

				*Restricting sample across regressions so obs are the same across specs
				ivregress 2sls `var'`rng' i.$fe $controls (evicted=$instrument) if  `pop'==`popval', r cluster($cluster_iv) 
				replace sample_restriction = e(sample)
				
				*Non-evict mean
				sum `var'`rng'   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_mean=r(mean)
				local y_base_sd= r(sd)
				unique pik   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_n= r(sum)
				
				sum `var'`rng'   if sample_restriction==1  & `pop'==`popval'
				if "`pop'" == "all" local sd_y_all = r(sd)
				if "`pop'" != "all" local sd_y_all = .
				sum evicted  if sample_restriction==1 & `pop'==`popval'
				if "`pop'" == "all" local sd_evic_all = r(sd)
				if "`pop'" != "all" local sd_evic_all = .
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng'"
				frame post results ("`label'")  (.) (.) (.) (`y_base_mean') (`y_base_sd') (`y_base_n') (`sd_y_all') (`sd_evic_all')  (.) (.)
				
				*OLS with additional controls
				reg  `var'`rng' evicted $controls `lags' i.$fe   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_ols)
				local b_evicted= _b[evicted]
				local se_evicted=_se[evicted] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Specification: OLS with additional controls" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
			
				*RF with additional controls	
				reg `var'`rng' $instrument $controls `lags' i.$fe   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_iv) 
				local b_evicted= _b[$instrument]
				local se_evicted=_se[$instrument] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Specification: RF with additional controls" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)

				*IV: Baseline with additional controls
				ivregress 2sls `var'`rng' $controls `lags' i.$fe  (evicted=$instrument)   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_iv) 
				local b_evicted= _b[evicted]
				local se_evicted=_se[evicted] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Specification: IV with additional controls" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
				
				if "`pop'" == "all" {
					
					*IV: one instrument, one endogenous variable, control for amount stringency
					ivregress 2sls `var'`rng' (evicted=$instrument) i.$fe $amnt_instrument $controls `lags' if sample_restriction == 1 & `pop'==`popval' & joint==1  & judgment_amount~=., r cluster($cluster_iv) 
					local b_evicted= _b[evicted]
					local se_evicted=_se[evicted] 
					local n_obs=e(N)
					local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Robustness: IV with additional controls, one instrument, one endogenous variable, control for amount stringency"
					frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
					
				}
				}
			  }
		}
	}
}

frame results: export delimited using "${Inputs}/Cook_hmis_ivols.csv", replace 

import delimited "${Inputs}/Cook_hmis_ivols.csv", clear 

split label, parse(",")
rename (label1 label2 label3 label4) (population outcome time_period specification)
rename (b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evic_all) (evic_coeff evic_se obs mean_outcome_non_evic sd_outcome_non_evicted sd_outcome_full_sample sd_evic_full_sample)
drop *judgement*

export delimited using "${Inputs}/Cook_hmis_ivols.csv", replace 
